﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com http://www.cszi.com
// </copyright>
//------------------------------------------------------------------------------
#endregion

using System;
using System.IO;

namespace CS.Utility.FileManage
{
    /// <summary>
    ///   文件夹辅助类
    /// </summary>
    /// 
    /// <description class = "CS.Utility.FolderUtility">
    ///   
    /// </description>
    /// 
    /// <history>
    ///   2010/2/18 12:58:50 , zhouyu ,  创建	     
    ///  </history>
    public class FolderHelper
    {

        #region 属性

        /// <summary>
        /// 父路径,默认为运行环境路径
        /// </summary>
        public string FatherPath { get; set; }


        private string _archiveRoot = "Archives";
        /// <summary>
        /// 文档文件夹名字，默认Archives
        /// </summary>
        public string ArchiveRoot {
            get { return _archiveRoot; }
            set { _archiveRoot = value; }
        }

        /// <summary>
        /// 是否按时间格式化出存档里的文件夹，否时无时间格式的文件夹
        /// </summary>
        public bool IsDateFormat { get; set; }

        private string _dateFormater = "yyyyMMdd";
        /// <summary>
        /// 格式化样式器，默认为yyyyMMdd
        /// </summary>
        public string DateFormater
        {
            get { return _dateFormater; }
            set { _dateFormater = value; }
        }

        #endregion

        #region 构造 与初始化

    

        /// <summary>
        /// 默认构造,此时有默认的存档目录 Archives
        /// </summary>
        public FolderHelper()
        {
            if (string.IsNullOrEmpty(FatherPath) || !FatherPath.Contains(@":\"))
            {
                FatherPath = AppDomain.CurrentDomain.BaseDirectory;
            }
        }

        /// <summary>
        /// 给定父路径,此时无存档目录
        /// </summary>
        /// <param name="basePath"></param>
        public FolderHelper(string basePath)
        {
            _archiveRoot = "";
            FatherPath = basePath;
            if (!string.IsNullOrEmpty(FatherPath) && FatherPath.Contains(@":\"))
            {
                FatherPath = basePath;
            }
            else
            {
                throw new DirectoryNotFoundException("给定父路径格式错误");
            }
        }



        #endregion

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="isDateFolder">是否按日期格式命名</param>
        /// <returns>返回全路径字符串</returns>
        public string Create(bool isDateFolder)
        {
            IsDateFormat = isDateFolder;
            //构造全路径
            var  fullPath = IsDateFormat
                                ? String.Format(@"{0}\{1}\{2}\", FatherPath, _archiveRoot, DateTime.Now.ToString(_dateFormater))
                                : String.Format(@"{0}\{1}\", FatherPath, _archiveRoot);

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
            return fullPath;
        }


    }
}